package com.syezon.wifikey.news;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseFragment;
import com.syezon.wifikey.safe.SafeCheckActivity;
import com.syezon.wifikey.speed.SpeedTestResult;
import com.syezon.wifikey.utils.WifiUtil;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class SpeedTestResultFragment extends BaseFragment {

    public static final String ARG_TEST_RESULT = "test_result";

    @BindView(R.id.tv_wide)
    TextView mTvWide;
    @BindView(R.id.tv_delay_value)
    TextView mTvDelayValue;
    @BindView(R.id.tv_dl_value)
    TextView mTvDlValue;
    @BindView(R.id.tv_up_value)
    TextView mTvUpValue;
    @BindView(R.id.pb_video_level)
    ProgressBar mPbMovieLevel;
    @BindView(R.id.tv_video_value)
    TextView mTvVideoValue;
    @BindView(R.id.pb_short_video_level)
    ProgressBar mPbShortVideoLevel;
    @BindView(R.id.tv_short_video_value)
    TextView mTvShortVideoValue;
    @BindView(R.id.pb_game_level)
    ProgressBar mPbGameLevel;
    @BindView(R.id.tv_game_value)
    TextView mTvGameValue;

    private SpeedTestResult speedTestResult;

    public SpeedTestResultFragment() {
    }


    public static SpeedTestResultFragment newInstance(SpeedTestResult speedTestResult) {
        SpeedTestResultFragment fragment = new SpeedTestResultFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_TEST_RESULT, speedTestResult);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.app_news_layout_wifi_speedtest_result;
    }

    @Override
    protected void initViewsAndEvents() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            speedTestResult = arguments.getParcelable(ARG_TEST_RESULT);
            showResult();
        }
    }

    @OnClick({R.id.tv_wifi_safe_test})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_wifi_safe_test:
                String wifiSSID = WifiUtil.getWifiSSID();
                if (!TextUtils.isEmpty(wifiSSID)) {
                    Bundle bundle = new Bundle();
                    bundle.putString(SafeCheckActivity.ARG_SSID, wifiSSID);
                    readyGo(SafeCheckActivity.class, bundle);
                }
                break;
        }
    }

    private void showResult() {
        if (speedTestResult != null) {
            mTvWide.setText(String.format("本次测速结果相当于%dM宽带", (int) (speedTestResult.getWide() + 1)));
            DecimalFormat decimalFormat = new DecimalFormat("##.##");
            mTvDelayValue.setText(decimalFormat.format(speedTestResult.getDelay()));
            mTvDlValue.setText(decimalFormat.format(speedTestResult.getDl()));
            mTvUpValue.setText(decimalFormat.format(speedTestResult.getUp()));
            mPbMovieLevel.setProgress(speedTestResult.getMovieScore());
            mTvVideoValue.setText(speedTestResult.getMovieScore() + " 分");
            mPbShortVideoLevel.setProgress(speedTestResult.getShortVideoScore());
            mTvShortVideoValue.setText(speedTestResult.getShortVideoScore() + " 分");
            mPbGameLevel.setProgress(speedTestResult.getGameScore());
            mTvGameValue.setText(speedTestResult.getGameScore() + " 分");
        }
    }

}
